package com.ustarlink.windowzoomanimations;

import android.app.ActivityOptions;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        GridView grid = (GridView) findViewById(R.id.grid);
        grid.setAdapter(new HoloTilesAdapter());
    }

    public class HoloTilesAdapter extends BaseAdapter{
        private static final int TILES_COUNT = 60;

        private final int[] COLORS = {getResources().getColor(R.color.blue),
            getResources().getColor(R.color.green),
            getResources().getColor(R.color.purple),
            getResources().getColor(R.color.red),
            getResources().getColor(R.color.yellow)
        };

        private final int[] DRAWABLES = {R.drawable.blue_tile,
            R.drawable.green_tile,
            R.drawable.purple_tile,
            R.drawable.red_tile,
            R.drawable.yellow_tile
        };


        @Override
        public int getCount() {
            return TILES_COUNT;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }


        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView v;
            if (convertView == null){
                v = (ImageView) getLayoutInflater().inflate(R.layout.holo_tile, parent, false);
            } else {
                v = (ImageView) convertView;
            }

            final int colour = COLORS[position % 5];
            v.setImageDrawable(getResources().getDrawable(DRAWABLES[position % 3]));
            v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View viwe) {
                    Intent i = new Intent(MainActivity.this, DetailActivity.class);
                    i.putExtra(DetailActivity.EXTRA_COLOUR, colour);
                    Log.i("TAG", colour + "");
                    Bundle b = null;
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN){
                        Bitmap bitmap = Bitmap.createBitmap(viwe.getWidth(), viwe.getHeight(),
                                Bitmap.Config.ARGB_8888);
                        bitmap.eraseColor(colour);

                        ActivityOptions.makeThumbnailScaleUpAnimation(viwe, bitmap, 0, 0).toBundle();
                        startActivity(i, b);
                    }
                }
            });

            return v;
        }
    }
}
